package com.ganwei.mybatisplus.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganwei.mybatisplus.config.util.ExcelUtil;
import com.ganwei.mybatisplus.config.util.PageData;
import com.ganwei.mybatisplus.system.mapper.db2.UsersMapper2;
import com.ganwei.mybatisplus.system.pojo.Users;
import com.ganwei.mybatisplus.system.service.UsesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class UsersServiceImpl extends ServiceImpl<UsersMapper2,Users> implements UsesService{

    @Autowired
    private UsersMapper2 userMapper;

    @Override
    public PageData<Users> queryUsersPage(Integer pageNum , Integer pageSize) {

        PageData<Users> result;
        //这里页数和页面条数，我写死了，有需要自行修改

        List<?> usersData =  userMapper.userPage(pageNum,pageSize);
        //获取分页数据
        List<Users> usersList = (List<Users>) usersData.get(0);
        //获取总条数
        int totalRecord = ((List<Integer>)usersData.get(1)).get(0);

        result = new PageData<>(usersList);
        result.setPageInfo(pageNum, pageSize, totalRecord);

        return result;
    }

    @Override
    public List<Users> queryAll(HttpServletResponse response) throws IOException {
        long start = System.currentTimeMillis();
        List<Users> usersList = userMapper.selectList(null);
        String[] titles = {"id", "用户姓名", "perms", "创建时间","更新时间","版本号","是否有效"};
        String[] keys = {"id", "用户姓名", "perms", "createTime", "updateTime", "version", "deleted"};
        log.debug("导出excel所花时间：" + (System.currentTimeMillis() - start));
        ExcelUtil.exportObjectExcel("导出用户", "用户信息表", titles, usersList, keys, response, "yyyy-MM-dd HH:mm:ss");
        return userMapper.selectList(null);
    }

    @Override
    public boolean insertAll(List<Users> users) {
        return this.saveBatch(users);
    }

    @Override
    public int updateUser(Users users) {
        return userMapper.updateById(users);
    }

    @Override
    public List<Users> userAll(QueryWrapper wrapper) {
        return userMapper.selectList(null);
    }

    @Override
    public Users userById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public int usersSave(Users users) {
        return userMapper.insert(users);
    }

}
